package com.rainman.noswagger.def_interface;

import com.rainman.noswagger.core.MethodTagWorkerInterface;
import com.rainman.noswagger.info_bean.BackInfoBean;
import com.rainman.noswagger.info_bean.ClassInfoBean;
import com.rainman.noswagger.info_bean.FiledInfoBean;
import com.rainman.noswagger.info_bean.MethodInfoBean;

public class MethodTagBackWorker implements MethodTagWorkerInterface<BackInfoBean> {
    @Override
    public BackInfoBean dealTag(MethodInfoBean methodInfoBean, String info, String lineComment) {


        if(info.equals("null")){
            return null;
        }else if(info.equals("{}")){
            return null;
        }else if(info.equals("[]")){
            methodInfoBean.setReturnDataArr(true);
            return null;
        }

        if(info.startsWith("[")){

            String infoRe=info.replaceFirst("\\[","");
            infoRe=infoRe.replaceFirst("]","");
            methodInfoBean.setReturnDataArr(true);
            ClassInfoBean classInfoBean = methodInfoBean.getCore().getClassInfo(infoRe);

            if(classInfoBean==null){
                BackInfoBean backInfoBean=new BackInfoBean();
                backInfoBean.setDescStr(info);
                methodInfoBean.getReturnBeanList().add(backInfoBean);
                return null;
            }
            for(FiledInfoBean filedInfoBean:classInfoBean.getFiledInfoBeanList()){

                BackInfoBean backInfoBean=new BackInfoBean();
                backInfoBean.setName(filedInfoBean.getName());
                backInfoBean.setTypeClassName(filedInfoBean.getTypeClassName());
                backInfoBean.setDescStr(filedInfoBean.getDescStr());
                methodInfoBean.getReturnBeanList().add(backInfoBean);
            }

        }else if(info.startsWith("{")){

            String infoRe=info.replaceFirst("\\{","");
            infoRe=infoRe.replaceFirst("}","");

            ClassInfoBean classInfoBean = methodInfoBean.getCore().getClassInfo(infoRe);

            if(classInfoBean==null){
                BackInfoBean backInfoBean=new BackInfoBean();
                backInfoBean.setDescStr(info);
                methodInfoBean.getReturnBeanList().add(backInfoBean);
                return null;
            }
            for(FiledInfoBean filedInfoBean:classInfoBean.getFiledInfoBeanList()){

                BackInfoBean backInfoBean=new BackInfoBean();
                backInfoBean.setName(filedInfoBean.getName());
                backInfoBean.setTypeClassName(filedInfoBean.getTypeClassName());
                backInfoBean.setDescStr(filedInfoBean.getDescStr());
                methodInfoBean.getReturnBeanList().add(backInfoBean);
            }

        }else {

            //methodInfoBean.setReturnSingleData(true);

            String[] s = info.split(" ");

            if(s.length<2){
                methodInfoBean.getCore().log("@back deal，less then 2 element skip"+lineComment);
                return null;
            }
            if(s.length==2){
                BackInfoBean backInfoBean=new BackInfoBean();
                backInfoBean.setTypeClassName(s[0]);
                backInfoBean.setDescStr(s[1]);
                methodInfoBean.getReturnBeanList().add(backInfoBean);
                return backInfoBean;
            }

            BackInfoBean backInfoBean=new BackInfoBean();
            backInfoBean.setTypeClassName(s[0]);
            String nameAndDescInfo = info.substring(s[0].length()+1);

            String[] s1 = nameAndDescInfo.trim().split(" ");
            if(s1.length<=0){
                return null;
            }

            backInfoBean.setName(s1[0]);

            String[] s2 = nameAndDescInfo.substring(s1[0].length()).trim().split(" ");

            if(s2.length>0){
                backInfoBean.setDescStr(s2[0]);
            }

            methodInfoBean.getReturnBeanList().add(backInfoBean);
            return backInfoBean;
        }

        return null;
    }

    @Override
    public String tagName() {
        return "@back";
    }


}
